// 武器系统 - 管理所有武器和射击逻辑
class WeaponSystem {
    constructor(scene) {
        this.scene = scene;
    }

    fireAtTarget(target, weapons, playerStats) {
        weapons.forEach(weapon => {
            this.fireWeapon(weapon, target, playerStats);
        });
    }

    fireWeapon(weaponType, target, playerStats) {
        const angle = Phaser.Math.Angle.Between(
            this.scene.player.x, this.scene.player.y,
            target.x, target.y
        );

        switch (weaponType) {
            case 'pistol':
                // 手枪 - 应用多重射击
                for (let i = 0; i < playerStats.multishot; i++) {
                    const spreadAngle = angle + (i - (playerStats.multishot - 1) / 2) * 0.1;
                    this.createBullet('bullet', spreadAngle, 300 * playerStats.projectileSpeed, playerStats.damage, playerStats);
                }
                break;
            case 'shotgun':
                // 散弹枪 - 3发子弹扇形射击，应用多重射击
                for (let shot = 0; shot < playerStats.multishot; shot++) {
                    for (let i = -1; i <= 1; i++) {
                        const spreadAngle = angle + (i * 0.3) + (shot * 0.05);
                        this.createBullet('shotgun_bullet', spreadAngle, 250 * playerStats.projectileSpeed, playerStats.damage * 0.7, playerStats);
                    }
                }
                break;
            case 'rifle':
                // 步枪 - 应用多重射击
                for (let i = 0; i < playerStats.multishot; i++) {
                    const spreadAngle = angle + (i - (playerStats.multishot - 1) / 2) * 0.05;
                    this.createBullet('rifle_bullet', spreadAngle, 400 * playerStats.projectileSpeed, playerStats.damage * 1.5, playerStats);
                }
                break;
            case 'machinegun':
                // 机枪 - 快速连发，应用多重射击
                for (let i = 0; i < playerStats.multishot; i++) {
                    setTimeout(() => {
                        this.createBullet('bullet', angle + (Math.random() - 0.5) * 0.2, 350 * playerStats.projectileSpeed, playerStats.damage, playerStats);
                    }, i * 50);
                }
                break;
            case 'laser':
                // 激光武器 - 穿透所有敌人，应用多重射击
                for (let i = 0; i < playerStats.multishot; i++) {
                    const spreadAngle = angle + (i - (playerStats.multishot - 1) / 2) * 0.08;
                    this.createLaserBullet('laser_bullet', spreadAngle, 500 * playerStats.projectileSpeed, playerStats.damage * 1.2, playerStats);
                }
                break;
            case 'rocket':
                // 火箭发射器 - 范围伤害，应用多重射击
                for (let i = 0; i < playerStats.multishot; i++) {
                    const spreadAngle = angle + (i - (playerStats.multishot - 1) / 2) * 0.15;
                    this.createRocketBullet('rocket', spreadAngle, 200 * playerStats.projectileSpeed, playerStats.damage * 2, playerStats);
                }
                break;
            case 'electric':
                // 电击枪 - 连锁伤害，应用多重射击
                for (let i = 0; i < playerStats.multishot; i++) {
                    const spreadAngle = angle + (i - (playerStats.multishot - 1) / 2) * 0.1;
                    this.createElectricBullet('electric_bullet', spreadAngle, 300 * playerStats.projectileSpeed, playerStats.damage * 0.8, playerStats);
                }
                break;
        }
    }

    createBullet(bulletType, angle, speed, damage, playerStats) {
        const bullet = this.scene.bullets.create(this.scene.player.x, this.scene.player.y, bulletType);
        bullet.setVelocity(
            Math.cos(angle) * speed,
            Math.sin(angle) * speed
        );
        bullet.damage = damage;
        bullet.piercing = playerStats.piercing;
        bullet.piercedEnemies = 0;
        bullet.createdTime = this.scene.time.now; // 添加创建时间

        // 子弹发射火花效果
        this.scene.particles.emitParticleAt(this.scene.player.x, this.scene.player.y, 3);
    }

    createLaserBullet(bulletType, angle, speed, damage, playerStats) {
        const bullet = this.scene.bullets.create(this.scene.player.x, this.scene.player.y, bulletType);
        bullet.setVelocity(
            Math.cos(angle) * speed,
            Math.sin(angle) * speed
        );
        bullet.damage = damage;
        bullet.piercing = 999; // 激光穿透所有敌人
        bullet.piercedEnemies = 0;
        bullet.weaponType = 'laser';
        bullet.createdTime = this.scene.time.now; // 添加创建时间

        // 激光特效
        this.scene.particles.emitParticleAt(this.scene.player.x, this.scene.player.y, 5);
    }

    createRocketBullet(bulletType, angle, speed, damage, playerStats) {
        const bullet = this.scene.bullets.create(this.scene.player.x, this.scene.player.y, bulletType);
        bullet.setVelocity(
            Math.cos(angle) * speed,
            Math.sin(angle) * speed
        );
        bullet.damage = damage;
        bullet.piercing = 0; // 火箭不穿透
        bullet.piercedEnemies = 0;
        bullet.weaponType = 'rocket';
        bullet.createdTime = this.scene.time.now; // 添加创建时间

        // 火箭尾迹
        this.scene.trailParticles.emitParticleAt(this.scene.player.x, this.scene.player.y, 3);
    }

    createElectricBullet(bulletType, angle, speed, damage, playerStats) {
        const bullet = this.scene.bullets.create(this.scene.player.x, this.scene.player.y, bulletType);
        bullet.setVelocity(
            Math.cos(angle) * speed,
            Math.sin(angle) * speed
        );
        bullet.damage = damage;
        bullet.piercing = playerStats.piercing;
        bullet.piercedEnemies = 0;
        bullet.weaponType = 'electric';
        bullet.createdTime = this.scene.time.now; // 添加创建时间

        // 电击特效
        this.scene.particles.emitParticleAt(this.scene.player.x, this.scene.player.y, 4);
    }

    findNearestEnemy(player, enemies, range) {
        let nearest = null;
        let minDistance = range;

        enemies.children.entries.forEach(enemy => {
            const distance = Phaser.Math.Distance.Between(
                player.x, player.y,
                enemy.x, enemy.y
            );

            if (distance < minDistance) {
                minDistance = distance;
                nearest = enemy;
            }
        });

        return nearest;
    }
}